package com.masz.apipassenger.gray;

import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * controller 切面，设置ThreadLocal 变量
 */
@Aspect
@Component
public class RequestAspect {

    @Pointcut("execution(* com.masz.apipassenger.controller..*.*(..))")
    private void anyMethod(){

    }

    @Before(value = "anyMethod()")
    public void before(JoinPoint joinPoint){

        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String version = request.getHeader("version");
//
//        Map<String,String> map = new HashMap<>();
//        map.put("version",version);
//
//        RibbonParameters.set(map);

        // 灰度规则2
        if (version.equals("v1")) {
            // 这里设置的就是metaData 中注册的元数据信息
            RibbonFilterContextHolder.getCurrentContext().add("version", "v1");
        }

    }
}
